<template>
  <a-modal    :width='600'
  title="合同文件"
  :visible="visible"
  @ok="handleOk"
  :confirmLoading="confirmLoading"
  @cancel="handleCancel"
  >
      <h3  class='mb20  fontWeight'  v-if='currentEnt.length>0'>合同双方：{{currentEnt[0].name}} ——— {{currentEnt[1].name}}</h3>
      <table  border width='100%'  class='contract'>
        <tr>
          <th  >合同文件
              <a-tooltip>
                  <template slot="title">
                    合同文件可选择手动上传或点击制作合同自动生成。
                  </template>
                   <a-icon type="question-circle" />
              </a-tooltip>
          </th>
          <th  >合同附件
              <a-tooltip>
                  <template slot="title">
                    合同附件可上传多份并可更替。
                  </template>
                   <a-icon type="question-circle" />
              </a-tooltip>
          </th>
        </tr>
        <tr>
          <td  width="50%"> 
            <fileUpload    :onefile='true'  uptip='*仅支持上传一个文件'  :showUploadList='{showRemoveIcon: false }'  v-on:uploading='($event)=>{fileData = $event.fileList}'  v-on:removeFile='remove($event,1)'  :fileList='fileData'  ></fileUpload>

          </td>
          <td  width="50%"> 
            <fileUpload    v-on:uploading='($event)=>{fileData2 = $event.fileList}'  v-on:removeFile='remove($event,2)'  :fileList='fileData2' ></fileUpload>
          </td>
        </tr>
      </table> 
  </a-modal>
</template>

<script>
import  { submitContractFile,getDetailTransaction }  from  '@/api/order/index'
import  fileUpload  from  '@/components/common/fileUpload'

export default {
  name: 'contractFile',
  props:{
      visible:{
        type:Boolean,
        default:false
      },
 
  },
  data () {
    return {
        confirmLoading:false,
        isEdit:false,
        currentEnt:[],
        fileData:[],/*合同文件对应数据*/
        fileData2:[],/*合同附件对应数据*/
        /*
        上传文件所需字段设置  ordercode prjcode  transctioncode
        */
        form:{

        }, 
    }
  },
  methods:{  
    handleOk(e) {
      let that = this , url   
      /*合同文件 格式转换 */ 
      this.form.contractFileCode =  this.fileData[0]==undefined  ? null : (this.fileData[0].code || this.fileData[0].response.data.code) 
      
      if(this.fileData2.length>0){
        this.form.contractFileList = this.fileData2.map((item)=>{
          return {
            code:item.status == 'done' ?  (item.code || item.response.data.code) :'' 
          }
        })
      }
      else{
        this.form.contractFileList = []
      }
      if(this.isEdit == true){
        url='/op/contract/modify_contract' //修改 合同
      }
      if(this.isEdit == false){
        url='/op/contract/create_contract' //新增 合同
      }
      this.confirmLoading = true
      submitContractFile(url,this.form).then((res)=>{
          if(res.data.flag == true){ 
            this.$message.success(res.data.msg)
            this.$emit('handleOk')
          }
          this.confirmLoading = false
      })
    },
    handleCancel(e) {
      /*取消时文件数据清空*/
      this.fileData =[]
      this.fileData2 = [] 
      this.$emit('handleCancel') 
    },
 
    /*功能函数*/
    // 上传文件组件
    /*文件上传中 filelist为受控组件，需要响应自组件上传的文件才能继续上传*/
    uploading(e){
      console.log(e)
      this.fileData = e.fileList
    },
   
    uploading2(e){
      this.fileData2 = e.fileList
    },
    /*移除文件*/
    remove(e,num){
        if(num == 1){
          this.removeFile_fromList(e,this.fileData)  
        } 
        if(num == 2){
          this.removeFile_fromList(e,this.fileData2)
        }
    }, 
 
  },
  mounted(){
    this.$nextTick(()=>{ 
      this.$on('openDialog',function(data,type){
        /*文件数据清空*/
        this.fileData =[]
        this.fileData2 = [] 

        //获取合同双方企业
        this.currentEnt = data.entList
        /*交易code 获取详情，获取合同文件*/
        this.form.transactionCode = data.code 

        if(data.contractVO){
            /*编辑情况下，绑定当前合同code*/
            this.form.code = data.contractVO.code
            this.isEdit = true
            getDetailTransaction(this.form.transactionCode).then((res)=>{
              if(res.data.flag == true){
                  let data  = res.data.data 
                  /*合同文件已上传的情况下 回填。注意回填格式 { uid:"",code:"" }*/
                  if(data.contractVO.contractFile ){ 
                      let obj = data.contractVO.contractFile
                      this.fileData.push({uid:obj.code,code:obj.code,name:obj.name,status:'done',url:'/'})
                  }
                  /*合同附件 同*/
                  if(data.contractVO.contractFileList  && data.contractVO.contractFileList.length!=0){
                      this.fileData2 = data.contractVO.contractFileList.map(item=>{
                          return {
                                  uid:item.code,//uid ：default 默认类型回填 ；删除 
                                  code:item.code,//提交表单时 需要取跟上传文件处理后返回同样的code   上传文件后取code
                                  name: item.name,
                                  status: 'done',  
                          }
                      }) 
                  }
              }
           })
        }
        /*新增时初始化状态*/
        else{
            this.form.code = ''
            this.isEdit = false
        }

        /*订单code，prjcode回填*/
        this.form.orderCode = data.orderCode
        this.form.prjCode = data.prjCode
      }) 
    })
  },
  computed: {
    
  }, 
  components:{
    fileUpload
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.contract th,td{padding:20px;text-align: center;}
</style>
